package com.library.controller;

import com.library.pojo.Book;
import com.library.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;


    @RequestMapping("/allBook")
    public String list(Model model){
        List<Book> list=bookService.queryAllBook();
        System.out.println(list);
        model.addAttribute("list",list);
        return "allBook";
    }
    //跳转到添加书籍页面
    @RequestMapping("/toAddBook")
    public String toAddBook(){
        return "addBook";
    }
    //添加书籍，并跳转到显示书籍信息页面
    @RequestMapping("/addBook")
    public String addBook(Book book){
        System.out.println("addBook=>"+ book);
        bookService.addBook(book);
        return "redirect:/book/allBook";
    }
    //跳转到查询结果页面
    @RequestMapping("/toQueryBook")
    public String toFindBook(String queryBookName, Model model){
        List<Book> books = bookService.queryBookByName(queryBookName);
        model.addAttribute("books",books);
        return "theBook";
    }
    //跳转到修改书籍页面
    @RequestMapping("/toUpdateBook")
    public String toUpdatePage(int id, Model model){
        Book book = bookService.queryBookById(id);
        model.addAttribute("book", book);
        return "updateBook";
    }
    //修改书籍，并跳转到显示书籍信息页面
    @RequestMapping("/updateBook")
    public String updateBook(Book book){
        System.out.println("updateBook=>"+ book);
        bookService.updateBook(book);
        return "redirect:/book/allBook";
    }
    //删除书籍，重新查询所有书籍
    @RequestMapping("/deleteBook")
    public String deleteBook(int id){
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }
}
